#!/usr/bin/env bash

if [ $(whoami) != 'azuracast' ]; then
    echo 'This script must be run as the "azuracast" user. Rerunning...'
    exec gosu azuracast azuracast_install "$@"
fi

echo "AzuraCast Setup"

APPLICATION_ENV="${APPLICATION_ENV:-production}"
echo "(Environment: $APPLICATION_ENV)"

if [ "$APPLICATION_ENV" != "production" ]; then
    if [ "$APPLICATION_ENV" = "testing" ]; then
        sudo mkdir -p vendor
        sudo chmod -R 0744 vendor
        sudo chown -R azuracast:azuracast vendor
    fi

    composer install
fi

exec azuracast_cli azuracast:setup "$@"
